package com.huike.web.controller.system;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.huike.clues.domain.HkBusiness;
import com.huike.clues.dto.HkBusinessDto;
import com.huike.clues.service.IHkBusinessService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 商机;Controller
 *
 * @author ruoyi
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/business")
public class HkBusinessController extends BaseController {
    @Autowired
    private IHkBusinessService hkBusinessService;


    /**
     * 查询商机;列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/list")
    public TableDataInfo list( HkBusinessDto hkBusiness) {
        startPage();
        List<HkBusiness> list = hkBusinessService.selectHkBusinessList(hkBusiness);
        return getDataTable(list);
    }

    /*
     * 公海池的查询
     * */
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/pool")
    public TableDataInfo listpool(HkBusinessDto hkSeaPool) {
        startPage();
        List<HkBusiness> list = hkBusinessService.selectHkSeaPoolList(hkSeaPool);
        return getDataTable(list);
    }

    /*
    公海池的添加
    */

    @PutMapping("/back/{id}/{backReason}")
    public AjaxResult Genjid(@PathVariable Long id, @PathVariable Long backReason) {
        HkBusiness es = hkBusinessService.selectHkBusinessById(id);
        es.setCreatedTime(LocalDateTime.now());
        es.setRejectReason(backReason);//添加它的回海原因
        es.setStatus(4L);//吧状态改为公海
        try {
            hkBusinessService.updateHkBusiness(es);
        } catch (Exception e) {
            throw new RuntimeException("此用户已经退回公海,不可添加");

        }
        return new AjaxResult(200, "操作成功", null);
    }

    /*
     * 公海的捞取
     * */
    @PutMapping("/gain")
    public AjaxResult Fishing(@RequestBody Map ids) {


        hkBusinessService.Fishing(ids);

        return new AjaxResult(200, "操作成功", null);
    }


    /**
     * 导出商机;列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:export')")
    @Log(title = "商机;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HkBusinessDto hkBusiness) {
        List<HkBusiness> list = hkBusinessService.selectHkBusinessList(hkBusiness);
        ExcelUtil<HkBusiness> util = new ExcelUtil<HkBusiness>(HkBusiness.class);
        util.exportExcel(list, "商机;数据");
    }

    /**
     * 获取商机;详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(hkBusinessService.selectHkBusinessById(id));
    }

    /**
     * 新增商机;
     */
    @PreAuthorize("@ss.hasPermi('system:business:add')")
  @Log(title = "商机;", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HkBusiness hkBusiness) {

        return toAjax( hkBusinessService.insertHkBusiness(hkBusiness));
    }

    /**
     * 修改商机;
     */
    @PreAuthorize("@ss.hasPermi('system:business:edit')")
    @Log(title = "商机;", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HkBusiness hkBusiness) {
        return toAjax(hkBusinessService.updateHkBusiness(hkBusiness));
    }

    /**
     * 删除商机;
     */
    @PreAuthorize("@ss.hasPermi('system:business:remove')")
    @Log(title = "商机;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hkBusinessService.deleteHkBusinessByIds(ids));
    }
}
